package com.photoeditor.demo.util;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator on 2017/7/5 0005.
 */

public class HolidayUtil {

    private static final String dataBaseName = "holiday.db";
    private static final String tableName = "holiday";
    public final static int DB_VERSION_HOLIDAY = 1;

    private SQLiteDatabase mHolidayDateBase;

    public HolidayUtil(){
        mHolidayDateBase = AssertDataBaseManager.getManager().getDatabase(dataBaseName);
    }

    //date : MM-dd
    public String getHoliday(String country,String year, String date){
        String holiday = "";
        if(mHolidayDateBase != null){
            Cursor cursor = mHolidayDateBase.query(tableName, new String[]{"name"}, "country = ? and year = ? and startDate = ?", new String[]{country, year, date}, null, null, null);
            if (cursor.moveToNext()) {
                holiday = cursor.getString(cursor.getColumnIndex("name"));
            }
            cursor.close();
        }
        return holiday;
    }

    public String getHolidayToday(){
        String country, year, date;
        country = country();
        Calendar c = Calendar.getInstance();
        year = String.valueOf(c.get(Calendar.YEAR));
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        date = sdf.format(new Date());
        return getHoliday(country, year, date);
    }

    public void onDestroy(){
        AssertDataBaseManager.getManager().closeDatabase(tableName);
    }

    private String country() {
        return Locale.getDefault().getCountry();
    }

}
